﻿angular.module('Caltech').controller('roleItemController', function ($scope, Restangular, $state, ngTableParams, $stateParams, toaster) {
    'use strict';

    $scope.model = {
        name: ''
    };

    if ($stateParams.id) {
        Restangular.one('role', $stateParams.id).get().then(function (model) {
            if (model) {
                $scope.model = model;
            }
        });
    }

    $scope.save = function (model) {
        return Restangular.all('role').post(model).then(function (data) {
            if (data != "false") {
                $state.go('role-item', { id: data.id });
            }
        });
    };


    $scope.remove = function (model) {
        return model.remove().then(function (data) {
                history.back();
        });
    };

    $scope.update = function (model) {
        Restangular.all('role')
          .customPUT(model, 'updateRoleName').then(function () {
              $state.forceReload();
          });
    };
});